/**
 * This file is part of the dspace-poltman project http://www.poltman.com.
 *
 * This is free software; you can redistribute it and/or modify it under
 * the terms of DuraSpace DSpace license detailed in the LICENSE and NOTICE 
 * files at the root of the source tree and available online at
 * http://www.dspace.org/license/
 * 
 */
package com.poltman.dscentral.core.vm.menu;

import java.util.Map;

import org.apache.log4j.Logger;
import org.zkoss.zkplus.spring.SpringUtil;

import com.poltman.dscentral.core.CoreSessionData;
import com.poltman.dscentral.db.exceptions.RepositoryAccessException;
import com.poltman.dscentral.db.exceptions.RepositoryException;
import com.poltman.dscentral.db.service.pm.MainMenuLabelsService;


/**
 * 
 * @author z.ciok@poltman.com
 * @version $Revision: 1767 $
 *
 */
public class MainMenuCommon  {
	private static Logger log = Logger.getLogger(MainMenuCommon.class);
	
	private CoreSessionData coreSessionData = null;

	protected String ml(String key) {                               
		CoreSessionData  coreSessionData = (CoreSessionData) SpringUtil.getBean("coreSessionData");
		String languageCode = coreSessionData.getSelectedLanguageCode();
		MainMenuLabelsService  mainMenuLabelsService = (MainMenuLabelsService) SpringUtil.getBean("mainMenuLabelsService");
		String ml = null;
		try {
			ml = mainMenuLabelsService.findByKeyAndLanguageCode(key, languageCode).getValue();
		} catch (RepositoryAccessException rae) {
			log.error("MainMenuCommon Error ", rae);
		} catch (RepositoryException e) {
			log.error("MainMenuCommon Error ", e);
		}
		return ml;
	}
	
	
	@SuppressWarnings("unchecked")
	protected Boolean isItemDisabled(String menuItem) {
		
		if (coreSessionData == null) {
			coreSessionData = (CoreSessionData) SpringUtil.getBean("coreSessionData");
		}
		
		boolean isAdmin = (Boolean) coreSessionData.getAttribute("isAdmin");
		if (isAdmin) {
			return false;
		}
		
		Map<String, String> epersonMenuItemsMap = (Map<String, String>) coreSessionData.getAttribute("epersonMenuItemsMap");	
		String mItem = epersonMenuItemsMap.get(menuItem);
		if ((mItem != null) && (menuItem.equals(mItem))) {
			return false;
		}
		return true;
	}
}
